package com.handu.luna.core.domain.article;


import java.util.LinkedList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Transient;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.handu.luna.core.domain.BasePersistentDomain;



@Entity
public class ArticleCategory extends BasePersistentDomain<ArticleCategoryRepository>{

	@NotNull(message="分类长度不能为空")
	@Size(min=1,max=100, message="分类名称长度不能为空或超过100个字符。")
	private String name;  //分类名称
	private long parent_id;   //分类顶级id
	private String parentCollection;  //在孩子的分类上存储所有的  上级分类（即孙子的分类上包括 爷爷和父亲）
	
	

	public String getParentCollection() {
		return parentCollection;
	}
	public void setParentCollection(String parentCollection) {
		this.parentCollection = parentCollection;
	}

	@Min(0) 
	@Max(value=254,message="最大不能超过255")
	private  int sort_order;  //排序
	private boolean is_show;  //是否显示
	private String keywords;  //关键字
	private String cat_desc;  // 描述  
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getParent_id() {
		return parent_id;
	}
	public void setParent_id(long parent_id) {
		this.parent_id = parent_id;
	}
	public int getSort_order() {
		return sort_order;
	}
	public void setSort_order(int sort_order) {
		this.sort_order = sort_order;
	}
	public boolean isIs_show() {
		return is_show;
	}
	public void setIs_show(boolean is_show) {
		this.is_show = is_show;
	}
	public String getKeywords() {
		return keywords;
	}
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
	public String getCat_desc() {
		return cat_desc;
	}
	public void setCat_desc(String cat_desc) {
		this.cat_desc = cat_desc;
	}
	
	@Transient
	private List<ArticleCategory> subCategories;
	public List<ArticleCategory> getSubCategories() {
		return subCategories;
	}
	public void setSubCategories(List<ArticleCategory> subCategories) {
		this.subCategories = subCategories;
	}
	public ArticleCategory(){  //构造函数
		
	}
	
	public ArticleCategory(long id,String name){
		super();
		this.id = id;
		this.name = name;
	}
	 public ArticleCategory(String name,long parent_id,String parentCollection,int sort_order,boolean is_show,String keywords,String cat_desc){
	    	super();
	    	this.name = name;
	    	this.parent_id = parent_id;
	    	this.parentCollection = parentCollection;
	    	this.sort_order = sort_order;
	    	this.is_show = is_show;
	    	this.keywords = keywords;
	    	this.cat_desc = cat_desc;
	  		
		}
    public ArticleCategory(long id,String name,long parent_id,String parentCollection,int sort_order,boolean is_show,String keywords,String cat_desc){
    	super();
    	this.id = id;
    	this.name = name;
    	this.parent_id = parent_id;
    	this.parentCollection = parentCollection;
    	this.sort_order = sort_order;
    	this.is_show = is_show;
    	this.keywords = keywords;
    	this.cat_desc = cat_desc;
  		
	}
    
    public void initSubArticleCategory(List<ArticleCategory> allArticleCategories){
    	if(this.subCategories==null){
    		this.subCategories = new LinkedList<ArticleCategory>();
    	}
    	for(ArticleCategory each : allArticleCategories){
    		if(this.getId() == each.getParent_id()){
    			this.subCategories.add(each);
    		}
    	}
    	for(ArticleCategory eachSub : subCategories){
    		eachSub.initSubArticleCategory(allArticleCategories);    		
    	} 	
    }
    
    public void addSelfToListArticle(List<ArticleCategory> categorieTableList){
    	categorieTableList.add(this);
    	if(this.subCategories!=null){
    		for(ArticleCategory eachSub : subCategories){
    			eachSub.addSelfToListArticle(categorieTableList);
    		}	
    	}	
    }
	
}
